<?php
/**
 * 图片管理
 */

namespace App\Http\Controllers\Admin;
use App\Common\Utill\Utills;
use App\Http\Controllers\Controller;
use App\Models\Pictures;
use App\Service\PictureService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class PictureController extends Controller
{
    public function index()
    {
        return view('admin.picture.index');
    }

    /**
     * 列表数据
     * @return void
     */
    public function listData(Request $request)
    {
        $num = $request->input('limit', self::ADMIN_PAGE_NUM);
        $server = new PictureService();
        $result = $server->getListData($request, $num);
        return Utills::resultData($result);
    }

    /**
     * 添加数据展示
     */
    public function createForm()
    {
        return view('admin.picture.create');
    }

    /**
     * 添加数据保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'thumbs'=>'required',
        ], [
            'thumbs.required' => '图片不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $thumbs = $request->input('thumbs',"");
        $server = new PictureService();
        $result = $server->create($thumbs);
        return Utills::resultData($result);
    }

    /**
     * 编辑展示
     * @return
     */
    public function editForm(Request $request)
    {
        $id = $request->input('id', 0);
        $data = Pictures::where(['id' => $id])->first();
        return view('admin.picture.edit')->with('data', $data);
    }

    /**
     * 编辑保存
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'thumbs'=>'required',
        ], [
            'thumbs.required' => '图片不能为空',
        ]);
        if ($validator->fails()) {
            $result = ['code' => 1, 'msg' => $validator->errors()->first()];
            return response()->json($result);
        }
        $id = $request->input('id', 0);
        $thumbs = $request->input('thumbs',"");
        $server = new PictureService();
        $result = $server->update($id,$thumbs);
        return Utills::resultData($result);
    }

    /*
    * 删除
    */
    public function delete(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error('删除失败');
        }
        $id = $request->input('id');
        $server = new PictureService();
        $result = $server->delOne($id);
        return Utills::resultData($result);
    }

    /*
     *全部删除
     */
    public function deleteAll(Request $request)
    {
        $idArr = $request->input('idList',"");
        $server = new PictureService();
        $result = $server->delAll($idArr);
        return Utills::resultData($result);
    }



}
